package com.shelpe.service.simbaproxy.dev;

import javax.servlet.http.HttpServletRequest;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.shelpe.service.simbaproxy.taobao.APIParameterBuilderFilter;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CompositeTBFilter extends ZuulFilter {

	private APIParameterBuilderFilter proxy;
	private APIParameterBuilderFilter sandboxProxy;
	
	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();
		return (ctx.getRouteHost() != null && !ctx.containsKey("error.status_code"));
	}

	@Override
	public Object run() {
		 HttpServletRequest req = RequestContext.getCurrentContext().getRequest();
		 String nick = req.getParameter("nick");
		 if(nick != null && nick.indexOf("sandbox_") > -1){
			 log.debug("go to sanbox: {}", nick);
			 this.sandboxProxy.run();
			 return null;
		 }
		return this.proxy.run();
	}

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return 38;
	}

	public void setProxy(APIParameterBuilderFilter proxy) {
		this.proxy = proxy;
	}

	public void setSandboxProxy(APIParameterBuilderFilter sandboxProxy) {
		this.sandboxProxy = sandboxProxy;
	}

	
}
